#include "dmr_gpio.h"

void dmr_gpioWritePinLevel(GPIO_Module* gpiox,uint16_t pin,bool status)
{
	if(status)
	{
		gpiox->PBSC = pin;
	}
	else
	{
		gpiox->PBC = pin;
	}
}

bool dmr_gpioReadPinLevel(GPIO_Module* gpiox,uint16_t pin)
{
	if ((gpiox->PID & pin) != (uint32_t)PIN_RESET)
	{
		return DMR_PIN_SET;
	}
		
	return DMR_PIN_RESET;
}


void dmr_gpioTogger(dmr_gpioHandle *handle)
{
	handle->gpiox->POD ^= handle->pin;
}



void dmr_gpioInit(dmr_gpioHandle *handle)
{
	EXTI_InitType EXTI_InitStructure;
    NVIC_InitType NVIC_InitStructure;
	
	uint32_t position = 0x00U;
    uint32_t current_pin = 0x00U;
	uint32_t _pinSortce = 0x00U;
	
	if(handle->gpiox == GPIOA)
	{
		RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);
//		_pinSortce = 0x00U;
	}
	else if(handle->gpiox == GPIOB)
	{
		RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOB);
		_pinSortce = 0x01U;
	}
	else if(handle->gpiox == GPIOC)
	{
		RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOC);
		_pinSortce = 0x02U;
	}
	else if(handle->gpiox == GPIOD)
	{
		RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOD);
		_pinSortce = 0x03U;
	}
	
	
	
	if(handle->isExit != DMR_DISABLE_EXIT)
	{
		RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO);
	}
	
	
	

    while(((handle->pin) >> position) != 0)
    {

        current_pin = (handle->pin) & (IO_POSITION_MASK << position);
        if(current_pin)
        {
            GPIO_Alternate_Set(handle->gpiox, handle->alternate, position);

            GPIO_Mode_Set(handle->gpiox, handle->mode, position);

            GPIO_Pull_Set(handle->gpiox, handle->pull, position);

            GPIO_SlewRate_Set(handle->gpiox, handle->speed, position);

            GPIO_Driver_Set(handle->gpiox, handle->current, position);
			
			if(handle->initLevel != DMR_PIN_INIT_LEVEL_DEFAULT)
			{
				dmr_gpioWritePinLevel(handle->gpiox,1 << position,handle->initLevel);
			}
        }
		
		if(handle->isExit != DMR_DISABLE_EXIT)
		{

			GPIO_EXTI_Line_Set(position,_pinSortce | position << 2);
			
			
//			GPIO_EXTI_Line_Set(position,handle->pinSource);
			
			/* Configure key EXTI line */
			EXTI_InitStructure.EXTI_Line    = handle->pin;
			EXTI_InitStructure.EXTI_Mode    = EXTI_Mode_Interrupt;
			EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; 
			EXTI_InitStructure.EXTI_LineCmd = ENABLE;
			EXTI_Peripheral_Initializes(&EXTI_InitStructure);

			/* Set key input interrupt priority */
			if(handle->pin == GPIO_PIN_0)
			{
				NVIC_InitStructure.NVIC_IRQChannel                   = EXTI0_IRQn;
			}
			
			else if(handle->pin == GPIO_PIN_1)
			{
				NVIC_InitStructure.NVIC_IRQChannel                   = EXTI1_IRQn;
			}
			
			else if(handle->pin == GPIO_PIN_2)
			{
				NVIC_InitStructure.NVIC_IRQChannel                   = EXTI2_IRQn;
			}
			
			else if(handle->pin == GPIO_PIN_3)
			{
				NVIC_InitStructure.NVIC_IRQChannel                   = EXTI3_IRQn;
			}
			
			else if(handle->pin == GPIO_PIN_4)
			{
				NVIC_InitStructure.NVIC_IRQChannel                   = EXTI4_IRQn;
			}
			
			else if(handle->pin >= GPIO_PIN_5 && handle->pin <= GPIO_PIN_9)
			{
				NVIC_InitStructure.NVIC_IRQChannel                   = EXTI9_5_IRQn;
			}
			
			else if(handle->pin >= GPIO_PIN_10 && handle->pin <= GPIO_PIN_15)
			{
				NVIC_InitStructure.NVIC_IRQChannel                   = EXTI15_10_IRQn;
			}
			
			
			NVIC_InitStructure.NVIC_IRQChannelSubPriority        = handle->priority;
			NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
			NVIC_Initializes(&NVIC_InitStructure);
		}
		
        position++;
    }
}







